#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using VisualDB.DAL;
using VisualDB.Helpers;
using Gizmox.WebGUI.Common.Resources;

#endregion

namespace VisualDB
{
    public partial class QueryUC : UserControl
    {
        private IDatabaseDAL _databaseDAL;

        public QueryUC(IDatabaseDAL databaseDAL, string query)
        {
            InitializeComponent();
            _databaseDAL = databaseDAL;
            this.txtQuery.Text = query;
        }

        public void ExecuteCurrentQuery()
        {
            string query;

            if (this.txtQuery.SelectionLength > 0)
            {
                query = this.txtQuery.SelectedText;
            }
            else
            {
                query = this.txtQuery.Text;
            }

            string errors;

            DataSet ds = _databaseDAL.ExecuteQuery(query, out errors);

            if (string.IsNullOrEmpty(errors))
            {
                this.tableLayoutPanel.Controls.Clear();

                foreach (DataTable table in ds.Tables)
                {
                    DataGridView grid = new DataGridView();
                    grid.AllowUserToAddRows = false;
                    grid.AllowUserToDeleteRows = false;
                    grid.AllowUserToOrderColumns = false;
                    grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                    grid.Dock = DockStyle.Top;
                    //grid.CellFormatting += new DataGridViewCellFormattingEventHandler(grid_CellFormatting);

                    this.tableLayoutPanel.Controls.Add(grid);
                    grid.DataMember = table.TableName;
                    grid.DataSource = table;
                }
            }

            this.txtMessages.Text = errors;

            if (errors != "")
            {
                this.tabControlResults.SelectedIndex = 1;

            }
            else
            {
                this.tabControlResults.SelectedIndex = 0;
            }
        }

        void grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;

            if (grid.Columns[e.ColumnIndex] is DataGridViewImageColumn)
            {
                

                //e.Value = new ResourceHandle
            }

            System.Diagnostics.Debug.Write("");
        }

        private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "ExecuteQuery":
                    ExecuteCurrentQuery();
                    break;
            }

        }

        private void QueryUC_Load(object sender, EventArgs e)
        {
            this.txtQuery.Focus();
        }
    }
}